
var countdown=60; 
function settime(obj) {	
    if (countdown == 0) { 
        obj.removeAttribute("disabled");    
        obj.value="免费获取验证码"; 
        countdown = 60; 
        return;
    } else { 
        obj.setAttribute("disabled", true); 
        obj.value="重新发送(" + countdown + ")"; 
        countdown--; 
    } 
setTimeout(function() { 
    settime(obj) }
    ,1000) 
}

function sendSms(obj){
	$("#msgbak").text("");
	var pass = sendSmsCode();
	if(pass){
		settime(obj);
	}
}

function checkForm() {
	var msgerr = ""; // 提示消息
	var iserr = true; // false 有错误 true 没有错误
	if ($("#accounts").val() == "") {
		iserr = false;
		msgerr += "请输入用户名!";
	} else if ($("#password").val() == "") {
		iserr = false;
		msgerr += "请输入密码!";
	}else if ($("#mycode").val() == "") {
		iserr = false;
		msgerr += "请输入验证码!";
	}
	if (!iserr) {
		$("#msgbak").text(msgerr);
	}else{
		if("admin"==$("#accounts").val()){
			// 密码加密
			md5Input("password");
		}
	}

	return iserr;
}

function md5Input(nodeId){
	// 密码加密
	var password = document.getElementById(nodeId).value;
	if(password.length!=16){
		var hash=hex_md5(document.getElementById(nodeId).value,16);
		document.getElementById(nodeId).value=hash.substring(8,24);
	}
	return 
}

function sendSmsCode() {
	if ($("#accounts").val() == "") {
		$("#msgbak").text('用户名为空');
		return false;
	}
	var pars = "accounts=" + $("#accounts").val();

	jQuery.ajax({
		type : "POST",
		url : "../login/getSmsCode.do",
		data : pars,
		dataType : 'text',
		success : function(results) {
			var result = eval(results);
			if (result.resultCode == '1') {
				$("#msgbak").text(result.message);
        	}else{
        		var message = result.message;
				if (message != "") {
					$("#msgbak").text(result.message);
				} else {
					$("#msgbak").text('发送信息失败');
				}
        	}
		}
	});
	return true;
}

function login(){
	if(checkForm()){
		jQuery.ajax({
			type : "POST",
			url : "../login/in.do?accounts="+$("#accounts").val()+"&password="+$("#password").val()+"&mycode="+$("#mycode").val(),
			data : {},
			dataType : "json",
			contentType : 'application/json; charset=UTF-8',
			async: false,
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#msgbak").text('登录失败！');
			},
			success : function(results) {
				var result = eval(results);
				if (result.resultCode == '1') {
					//$("#msgbak").text(result.message);
					window.location.href = "welcome.do";
	        	}else{
	        		var message = result.message;
					if (message != "") {
						$("#msgbak").text(result.message);
					} else {
						$("#msgbak").text('登录失败');
					}
	        	}
			}
		});
	}
}